using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Practices.RepositoryFactory.CodeGeneration.ObjectModel.DbTypesHelperStrategies
{
    /// <summary>
    /// Used by the templates to determine the DbTypeHelper strategy based
    /// on the provider as specified in the connection string.
    /// </summary>
    /// <remarks>
    /// This is really a factory method implementation and encapsulates the
    /// creation of DbTypesHelper strategies in a good enough manner.
    /// </remarks>
    public sealed class DbTypesHelperFactory
    {
        private static SqlServerDbTypesHelper _sqlServerDbTypesHelper = null;
        private static MsOraDbTypesHelper _msOraDbTypesHelper = null;
        private static OdpDbTypesHelper _odpDbTypesHelper = null;

        /// <summary>
        /// Used by the templates to determine the DbTypeHelper strategy based
        /// on the provider as specified in the connection string.
        /// </summary>
        /// <param name="databaseProvider">The database provider name.</param>
        /// <returns>A strategy that implements 
        /// the <see cref="IDbTypesHelperStrategy"/>.</returns>
        public static IDbTypesHelperStrategy Create(String databaseProvider)
        {
            if (String.Equals(databaseProvider, DatabaseProvider.SqlServer))
            {
                if (_sqlServerDbTypesHelper == null)
                    _sqlServerDbTypesHelper = new SqlServerDbTypesHelper();
                return _sqlServerDbTypesHelper;
            }

            if (String.Equals(databaseProvider, DatabaseProvider.MsOra))
            {
                if (_msOraDbTypesHelper == null)
                    _msOraDbTypesHelper = new MsOraDbTypesHelper();
                return _msOraDbTypesHelper;
            }

            if (String.Equals(databaseProvider, DatabaseProvider.ODP))
            {
                if (_odpDbTypesHelper == null)
                    _odpDbTypesHelper = new OdpDbTypesHelper();
                return _odpDbTypesHelper;
            }

            throw new Exception(
                String.Format("Database provider {0} not supported. Supported providers include:\n{1}\n{2}\n{3}"
                , databaseProvider, DatabaseProvider.SqlServer, DatabaseProvider.MsOra, DatabaseProvider.ODP));
        }
    }
}
